In [1]:
def __str__(self):
return unicode(self).encode('utf-8')
import time
In [2]:
x = range(1,10)
x
Out[2]:
In [3]:
def sum_number_mode(a,b,maxbarrier):
x = 0
for i in range(1,maxbarrier-1):
if i%a == 0 or i%b == 0:
x += i
return x
sum_number_mode(3,5,1001)
Out[3]:
In [4]:
x = [3,4]
x[1]
Out[4]:
In [6]:
def sum_even_fibonacci(mod, maxfibonacci):
x = [1,2]
sum_fib = 0
while x[1] < maxfibonacci:
if x[1]%mod == 0:
sum_fib += x[1]
y = x[1]
x[1] += x[0]
x[0] = y
return sum_fib
sum_even_fibonacci(2,40000000000)
Out[6]:
In [7]:
def greatest_factor(prime):
x = 2 # initiate first factor
while x != prime:
if prime%x == 0:
result = prime/x
prime = result
else:
x += 1
else:
return prime
greatest_factor(81)
Out[7]:
In [8]:
import time
start_time = time.time()
def palindrome(number):
return str(number) == str(number)[::-1]
list_number = []
for x in range(800,1000):
for i in range(800,1000):
result = i*x
if palindrome(result) == True:
list_number += [result,]
max(list_number)
print("--- %s seconds ---" % (time.time() - start_time))
In [9]:
start_time = time.time()
list_number = []
for x in range(800,1000):
for i in range(800,1000):
result = i*x
s = list(__str__(result))
if (s[0] == s[5]) and (s[1] == s[4]) and (s[2] == s[3]):
list_number += [result,]
max(list_number)
print("--- %s seconds ---" % (time.time() - start_time))
In [10]:
def smallest_divided(start, max_number):
factors = range(start, max_number + 1)
x = (len(factors)) - 1
factors_result = factors[0]
while (factors[x]-1) >= factors[0]:
if factors_result%factors[x-1] !=0 :
factors_result *= factors[x-1]
print factors_result
x -= 1
#return factors_result
smallest_divided(1,20)
In [14]:
def factoring(prime):
x = 2 # initiate first factor
factors = []
result = 0
while x != prime:
while (prime%x == 0):
result = prime/x
prime = result
factors.append(x)
if prime == 1:
break
if prime == 1:
break
else:
x += 1
else:
factors.append(prime)
return factors
factoring(20)
Out[14]:
In [26]:
def smallest_divided(start, max_number):
x = range(start, max_number + 1)
factors = []
for i in x:
z = factoring(i)
factors.append(z)
return factors
smallest_divided(1,20)
faktor = smallest_divided(1,20)
In [49]:
set_factor = faktor
def add_set(set_factor):
set_all = []
intersect = []
join = []
for i in set_factor:
intersect = i and set_all
i = i - intersect
set_all = i + set_all
return set_all
In [61]:
set_all_simulate = []
for i in set_factor:
set_all_simulate += i
print set_all_simulate
factor_set = set(set_all_simulate)
factor_set
z = 1
for i in factor_set:
z *= i
print z
In [51]:
set_factor
Out[51]:
In [82]:
def sum_of_squares(start, end):
sum_all = 0
for i in range(start, end +1):
sum_all += (i)**2
return sum_all
su_sq = sum_of_squares(1,100)
In [81]:
def square_of_sum(start, end):
sum_all_i = 0
b = 0
for i in range(start, end + 1):
sum_all_i += i
b = sum_all_i * sum_all_i
return b
sq_su = square_of_sum(1,100)
In [83]:
sq_su - su_sq
Out[83]:
In [ ]: